<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/25
 * Time: 10:24
 */
namespace Common\Model;
class CollectModel extends BaseModel{
    static $instance=null;
    private function getInstance(){
        if(!self::$instance){
            self::$instance=M('collection',null,DB_MAIN_CFG);
        }
        return self::$instance;
    }

    /**
     * 获取收藏对象信息
     */

    public static function getCollectState($aid,$uid){
        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
        if(!$aid){
            return $dataFailure;
        }
        $collectM = self::getInstance();
        $done=0;
        if($uid){
            $done=$collectM->where('aid='.$aid.' and uid='.$uid)->find()?1:0;
        }
        $dataSuccess['done']=$done;
        return $dataSuccess;
    }

    /**
     * 添加/取消收藏通用函数
     */
    public static function collectOP($aid,$uid){
        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');

        if(!$aid){
            return $dataFailure;
        }
        $collectM = self::getInstance();
        if($collectM->where('aid='.$aid.' and uid='.$uid)->find()){
            $collectM->where('aid='.$aid.' and uid='.$uid)->delete();
        }else{
            $data['aid'] = $aid;
            $data['uid'] = $uid;
            $data['collectTime']=time();
            $tmp = $collectM->add($data);
            if($tmp === false){
                $dataFailure['msg'] = "添加失败";
                return $dataFailure;
            }
        }
        return $dataSuccess;
    }

    /**
     * 获取收藏列表
     */
    public static function getMyCollection($uid,$page,$pagesize,$otherUid = 0){
        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
        if(!$uid){
            $dataFailure['msg']="请登录后操作";
            return $dataFailure;
        }

        $dataSuccess['page'] = $page ? $page :1;          //返回当前页
        $offset = ($dataSuccess['page']-1)*$pagesize;     //起始条数

        $list = self::getInstance()->where("uid={$uid}")->limit($offset,$pagesize)->select();
        $article = array();
        if($list) {
            foreach ($list as $value) {
                $article[] = M('article', null, DB_MAIN_CFG)->where("id=" . $value['aid'])->find();
            }
            $tagKv = getUTagKv();
            foreach ($article as $key => $value) {
                $tag = M('u_user_base', null, DB_MAIN_CFG)->where("uid=" . $value['uid'])->getField('userTag');
                $article[$key]['userTag'] = $tag;
                $article[$key]['userTagHtml'] = $tagKv[$tag];
                //喜欢
                if (!$otherUid) {
                    $article[$key]['done'] = M('praise_log', null, DB_MAIN_CFG)->where("objID=$value[id] and uid={$uid} and objType=" . OBJ_TYPE_ARTICLE)->find() ? 1 : 0;
                } else {
                    $article[$key]['done'] = M('praise_log', null, DB_MAIN_CFG)->where("objID=$value[id] and uid={$otherUid} and objType=" . OBJ_TYPE_ARTICLE)->find() ? 1 : 0;
                }

            }
        }

        $dataSuccess['data']=$article;

        $dataSuccess['total'] = count($article);         //获取数据总条数
        $dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pagesize);  //总页数
        $dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage']?0:1;    //是否还有下一页
        return $dataSuccess;
    }


}